/*
*		ÏßÐÔ»º³åÇøÊµÏÖ£¬²ÉÓÃË«¶Ë¶ÓÁÐ´æ´¢
*/

#include "CxLinerBuffer.h"

using namespace DataRevPart;


CxLinerBuffer::CxLinerBuffer(size_t  size):CxImageBuffer(size),m_effictive(true)
{
}
CxLinerBuffer::~CxLinerBuffer()
{
}


void CxLinerBuffer::Clear()
{
	_Mutex::scoped_lock lock(m_mu);	
	m_buffer.clear();
}




void CxLinerBuffer::Push(const Mat &img )
{
	try
	{
		{
			_Mutex::scoped_lock lock(m_mu);
			if(!m_effictive)return ;
			//bufferÂúµÈ´ý

			
			while(IsFull())
			{
				m_writecond.wait(m_mu);
			}
			

			m_buffer.push_front(img);

			//ÌáÐÑbufferµ÷ÓÃÕßbufferÒÑ¿ÉÒÔ´æ´¢
			m_readcond.notify_one();
		}
	}
	//threadÒì³£´¦Àí£¬ÍË³ö
	catch(boost::thread_interrupted &)
	{
		throw ;
	}
}

Mat		CxLinerBuffer::Pop()
{
	
	try
	{
		Mat  _tmp;
		{
			//¾Ö²¿Ëø
			_Mutex::scoped_lock lock(m_mu);
			if(!m_effictive)return Mat(0,0,CV_8UC3);
			//bufferÎª¿ÕµÈ´ý
			while(IsEmpty())
			{
				m_readcond.wait(m_mu);
				if(!m_effictive)
					return Mat(0,0,CV_8UC3);
			}
			
			_tmp=m_buffer.back();
			m_buffer.pop_back();
			//ÌáÐÑbufferµ÷ÓÃÕß¿ÉÒÔ¶ÁÈ¡
			m_writecond.notify_one();
		}
		return	_tmp;
	}
	//Òì³£´¦ÀíÍË³ö
	catch(const boost::lock_error &)
	{
		throw ;
	}
	catch(const boost::thread_interrupted &)
	{
		throw ;
	}
}
